let html = "";
$.ajax({
    url: "http://chst.vip:1234/api/getindexmenu"
}).then(res => {
    creatEl(res.result);
    // console.log($("li img"));
    $(".container ul").on("click", "li", clickHandler)
})


$.ajax({
    url: "http://chst.vip:1234/api/getmoneyctrl"
}).then(res => {
    creatDiscount(res.result);
})

$(".footer .more a").click(function () {
    location.href = "./pages/"
})



function creatEl(data) {
    data.forEach(item => {
        img = item.img.replace(/[\"].+?[\/]/, "\"./assets/imgs/");
        html += `
                    <li class="${item.titlehref}">
                        ${img}
                        <p>${item.name}</p>
                    </li>
                `;
    });
    $(".container ul").html(html);
}



function creatDiscount(data) {
    html = "";
    data.forEach(item => {
        // console.log(item);
        html += `
                     <li productId="${item.productId}">
                     ${item.productImgSm}
                     <p>${item.productName}</p>
                     <span>${item.productFrom}${item.productTime}</span>
                     <span>${item.productComCount}</span>
                     </li>
                `;
    });
    // console.log(html);
    $(".product ul").html(html);
    $(".product ul").on("click", "li", clickHandler)
}


function clickHandler(e) {
    let href = $(this).prop("class") == "" ? $(this).attr("productId") : $(this).prop("class");
    if (href == "#") {
        $(".container ul li:nth-child(n+9)").slideToggle(500);
    } else if (parseInt(href)) {
        location.href = "./pages/indexdetails.html?productid=" + href;
    } else {
        location.href = "./pages/" + href;
    }
}